﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using wdsDeApp2.Models;

namespace wdsDeApp2.Controllers
{
    public class MuHomeController : Controller
    {
        [HttpGet]
        public ActionResult Index(string carNO = null, string mobile = null)
        {
            using (Model1 model = new Model1())
            {
                #region 获取数据
                var listInfo = model.CarRecord.Where(u => true).ToList();
                if (!string.IsNullOrEmpty(carNO))
                    listInfo = listInfo.Where(u => u.CarNo.IndexOf(carNO) >= 0).ToList();
                if (!string.IsNullOrEmpty(mobile))
                    listInfo = listInfo.Where(u => u.Mobile.IndexOf(mobile) >= 0).ToList();
                #endregion

                #region 小区
                Dictionary<int, string> keyValuePairs = new Dictionary<int, string>();
                var cName = model.CommunityInfo.Where(u => true).ToList();
                foreach (var item in cName)
                    keyValuePairs.Add(item.CId, item.CommunityName);
                ViewBag.cName = keyValuePairs;
                #endregion


                return View(listInfo);
            }
        }

        [HttpGet]
        public ActionResult Create()
        {
            using (Model1 model = new Model1())
            {
                #region 小区名称
                var cList = model.CommunityInfo.Where(u => true).ToList();
                List<SelectListItem> e = new List<SelectListItem>();
                foreach (var item in cList)
                    e.Add(new SelectListItem() { Value = item.CId.ToString(), Text = item.CommunityName });
                ViewBag.cName = new SelectList(e, "Value", "Text");
                #endregion
                return View(new CarRecord());
            }
        }

        [HttpPost]
        public ActionResult Create(CarRecord carRecord)
        {
            if (!ModelState.IsValid) return Json(false);

            using (Model1 model = new Model1())
            {
                model.CarRecord.Add(carRecord);
                if (model.SaveChanges() == 1)
                    return RedirectToAction("Index", "MuHome");
                return Json(false);
            }
        }

        [HttpPost]
        public ActionResult Update(string CarId)
        {
            using (Model1 model = new Model1())
            {
                var carid = Convert.ToInt32(CarId);
                var info = model.CarRecord.FirstOrDefault(u => u.CarId == carid);
                info.Type = 1;
                return Json(model.SaveChanges());
            }
        }

        [HttpPost]
        public ActionResult Delete(string CarId)
        {
            using (Model1 model = new Model1())
            {
                var carid = Convert.ToInt32(CarId);
                model.CarRecord.Remove(model.CarRecord.FirstOrDefault(u => u.CarId == carid));
                return Json(model.SaveChanges());
            }
        }
    }
};